package com.freesky.quartz.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.freesky.biz.abchina.api.AbChinaCoupon;
import com.freesky.biz.abchina.domain.TXianglanRecord;
import com.freesky.biz.abchina.service.IXianglanRecordService;

import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.framework.web.service.SendAlarmService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 农行向岚活动
 */
@Component("abChinaXianglanTask")
public class AbChinaXianglanTask {
    private static final Logger logger = LoggerFactory.getLogger(AbChinaXianglanTask.class);

    @Autowired
    private RedisCache redisCache;


    @Autowired
    private IXianglanRecordService xianglanRecordService;

    @Autowired
    private AbChinaCoupon abChinaCoupon;

    @Autowired
    private SendAlarmService sendAlarmService;

    public void taskProcess() {
        logger.info("AbChinaXianglanTask task start");

        boolean nReloadFlag = false;
        List<TXianglanRecord> xianglanRecordList = xianglanRecordService.selectUnPayList();
        for(TXianglanRecord xianglanRecord : xianglanRecordList){
            AjaxResult queryResult = abChinaCoupon.userQuery(xianglanRecord.getPayOrderNo());
            if(!queryResult.isSuccess()){
                continue;
            }
            JSONObject jsonObject = JSON.parseObject(queryResult.get(AjaxResult.DATA_TAG).toString());
            xianglanRecord.setPayStatus(jsonObject.getString("status"));

            if(!"0".equalsIgnoreCase(xianglanRecord.getPayStatus())) {
                xianglanRecordService.updatePayStatus(xianglanRecord);
                nReloadFlag = true;
            }
        }

        if(nReloadFlag){
            xianglanRecordService.xianglanRecordInit();
        }
    }

}
